package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.DailyNews;
import cn.wolfcode.trip.base.query.DailyNewsQuery;
import cn.wolfcode.trip.base.service.IDailyNewsService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/newsContent")
public class NewsContentCntroller {
    @Autowired
    private IDailyNewsService dailyNewsService;

    /**
     * 根据传来的id查内容
     * @param news_id
     * @return
     */
    @GetMapping("/{news_id}/selectedNewsContent")
    public DailyNews getById(@PathVariable Long news_id){
        DailyNews dailyNews = dailyNewsService.selectByID(news_id);
        return dailyNews;
    }

    /**
     * 查询5篇catalog类似的文章,若果同类型的文章数量不够,就查询所有的文章
     */
    @GetMapping("/newses")
    public PageInfo<DailyNews> getByCatalog(DailyNewsQuery qo ,Long id){
        qo.setOrderBy("createTime desc");
        qo.setPageSize(5);
        PageInfo<DailyNews> dailyNewsPageInfo = dailyNewsService.selectByCatalog(qo,id);
        if(dailyNewsPageInfo.getList().size()<5){
            qo.setCatalog(-1);
            dailyNewsPageInfo = dailyNewsService.selectByCatalog(qo,id);
        }
        return dailyNewsPageInfo;
    }

}
